package com.zwj.batch.config.test;

import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.launch.support.RunIdIncrementer;
import org.springframework.batch.core.step.tasklet.Tasklet;
import org.springframework.batch.item.ExecutionContext;
import org.springframework.batch.repeat.RepeatStatus;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.Resource;
import java.util.Map;

/**
 * batch步骤流程控制练习
 * 普通Tasklet
 * @author 张文接
 * @version 1.0
 * @createTime 2023-05-28 19:52:00
 */
//@Configuration
public class BatchConfig08 {
    public static final String KEY_ITEM = "item";
    //job构造工厂
    @Resource
    private JobBuilderFactory jobBuilderFactory;
    //step构造工厂
    @Resource
    private StepBuilderFactory stepBuilderFactory;

    @Bean
    public Tasklet taskletStart() {
        return (stepContribution, chunkContext) -> {
            System.err.println("===============开始获取、校验参数=============");
            //获取参数
            Map<String, Object> parameters = chunkContext.getStepContext().getJobParameters();
            String itemStr = (String) parameters.get(KEY_ITEM);
            int item = Integer.parseInt(itemStr);
            //存入jobEC中
            ExecutionContext jobContext = chunkContext.getStepContext().getStepExecution().getJobExecution().getExecutionContext();
            jobContext.put(KEY_ITEM, item);
            System.err.println("获取参数item成功：" + item + "并塞入JOB上下文中");
            return RepeatStatus.FINISHED;
        };
    }

    /**
     * RepeatStatus.CONTINUABLE：继续执行当前步骤
     * RepeatStatus.FINISHED: 当前步骤结束
     *
     * @return
     */
    @Bean
    public Tasklet tasklet1() {
        return (stepContribution, chunkContext) -> {
            System.err.println("===============业务逻辑=============");
            //获取参数
            ExecutionContext jobContext = chunkContext.getStepContext().getStepExecution().getJobExecution().getExecutionContext();
            int item = jobContext.getInt(KEY_ITEM);
            if (item > 0) {
                System.err.println("任务正在执行，，，，" + item);
                jobContext.put(KEY_ITEM, item - 1);
                return RepeatStatus.CONTINUABLE;
            }
            return RepeatStatus.FINISHED;
        };
    }


    @Bean
    public Tasklet taskletEnd() {
        return (stepContribution, chunkContext) -> {
            System.err.println("===============结束=============");
            return RepeatStatus.FINISHED;
        };
    }

    @Bean
    public Job job() {
        return jobBuilderFactory
                .get("step-chunk-tasklet-job")
                .incrementer(new RunIdIncrementer())
                .start(stepStart())
                .next(step1())
                .next(stepEnd())
                .build();
    }

    @Bean
    public Step step1() {
        return stepBuilderFactory
                .get("step1")
                .tasklet(tasklet1())
                .build();
    }

    @Bean
    public Step stepStart() {
        return stepBuilderFactory
                .get("stepStart")
                .tasklet(taskletStart())
                .build();
    }

    @Bean
    public Step stepEnd() {
        return stepBuilderFactory
                .get("stepEnd")
                .tasklet(taskletEnd())
                .build();
    }

}
